package com.wlts.wzy.DYOrderModule.util;

import com.alibaba.fastjson.JSON;
import com.wlts.wzy.DYGoodsModule.DYapi.DySdk;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYOrderModule.service.IDyOrdersService;
import com.wlts.wzy.DYUserModule.entity.DyOauth;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyOauthService;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.api.request.dy.LogisticsAddToDyApiRequest;
import com.wlts.wzy.api.response.dy.LogisticsAddToDyApiResponse;
import com.wlts.wzy.api.vo.DyVo.DyPack;
import com.wlts.wzy.api.vo.DyVo.DyShippedOrder;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Component
public class LmExpressToDyUtil {
    @Autowired
    private IDyOrdersService ksOrdersService;
    @Autowired
    private IDyStoreUserService ksStoreUserService;
    @Autowired
    private IDyOauthService ksOauthService;

    /**
     *  推送订单信息到抖音
     */
    @Scheduled(fixedDelay = 60 * 1000)
//    @PostConstruct
    public void LmExpressToDy(){


        System.out.println("推送物流单到抖音 计时器");

        MyLambdaQueryWrapper<DyStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(DyStoreUser::getDelFlag, "0");

        userWrapper.isNotNull(DyStoreUser::getDySellerId);

        List<DyStoreUser> ksOauthList = ksStoreUserService.list(userWrapper);

        for (DyStoreUser user : ksOauthList
        ) {
            MyLambdaQueryWrapper<DyOrders> queryWrapper = new MyLambdaQueryWrapper<>();

            queryWrapper.in(DyOrders::getSelfOrder, 1);//平台商品的订单 0未确定 1是 2不是

            queryWrapper.eq(DyOrders::getPutLmStatus, 1);//推送lm状态 0未推送 1已推送 2推送失败

            queryWrapper.eq(DyOrders::getSendStatus, 1);//lm发货状态 0发货 1已发货

            queryWrapper.eq(DyOrders::getPutExpressStatus, 0);//推送物流状态 0未推送，1已经推送 2推送失败

            queryWrapper.eq(DyOrders::getShopId, user.getDySellerId());

            queryWrapper.isNotNull(DyOrders::getLmOrderId);

            queryWrapper.isNotNull(DyOrders::getExpressNo);

            MyLambdaQueryWrapper<DyOauth> oauthWrapper = new MyLambdaQueryWrapper<>();

            oauthWrapper.eq(DyOauth::getDelFlag, "0");

            oauthWrapper.eq(DyOauth::getUserUuid, user.getUuid());

            DyOauth ksOauth = ksOauthService.getOne(oauthWrapper);



            int page = 1;
            int pageSize = 15;

            while (true) {

                List<DyOrders> ksOrdersList = ksOrdersService.page(queryWrapper, page, pageSize, true).getRecords();

               // System.out.println(ksOauthList);

                ksOrdersList.forEach(ksOrders -> {

                    LogisticsAddToDyApiRequest request=new LogisticsAddToDyApiRequest();
                    request.setOrder_id(ksOrders.getPid());
                    request.setRequest_id(UUID.randomUUID().toString());

                    DyPack pack = new DyPack();

                    DyShippedOrder shippedOrder = new DyShippedOrder();

                    shippedOrder.setShipped_num(ksOrders.getComboNum());
                    shippedOrder.setShipped_order_id(ksOrders.getOrderId());

                    List<DyShippedOrder> shippedOrders=new ArrayList<>();
                    shippedOrders.add(shippedOrder);

                    pack.setShipped_order_info(shippedOrders);

                    pack.setLogistics_code(ksOrders.getExpressNo());
                    pack.setLogistics_id(ksOrders.getExpressCode() + "");

                    List<DyPack>  dyPackList=new ArrayList<>();
                    dyPackList.add(pack);
                    request.setPack_list(JSON.toJSONString(dyPackList));
                    LogisticsAddToDyApiResponse response;
                    try {
                        response = DySdk.LogisticsAddSinglePack(user.getAppKey(), user.getAppSecret(), ksOauth.getAccessToken(), request);
                        if (response.getErr_no() == 0) {
                            ksOrders.setPutExpressStatus(1);
                        } else {
                            ksOrders.setPutExpressStatus(2);
                        }
                        ksOrders.setKsErrMsg(JSON.toJSONString(response));
                    }catch (Exception e){
                        ksOrders.setPutExpressStatus(2);
                        ksOrders.setKsErrMsg(e.toString());
                    }
                });
                if (ksOrdersList.size() > 0) {
                    ksOrdersService.updateBatchById(ksOrdersList);
                }

                if (ksOrdersList.size() < pageSize) {
                    break;
                }
                page += 1;
            }
        }

    }

}
